#include "functionFitness.h"

vector<things> set;
double maxW = 5;
double penalization = 5;
/**
	Carga los datos del txt
*/
void loadThings(string file){
	ifstream archi;
	archi.open(file.c_str());
	if(archi.is_open()){
		while(!archi.eof()){
			things aux;			
			string str_line;
			char* line;
			//Cantidad de cromosoma
			getline(archi,str_line);
			aux.name = str_line;
			cout<<"nombre: "<<aux.name<<endl;
			getline(archi,str_line);
			line = &str_line[0];
			aux.g = atof(line);
			cout<<"ganancia: "<<aux.g<<endl;
			getline(archi,str_line);
			line = &str_line[0];
			aux.w = atof(line);
			cout<<"peso: "<<aux.w<<endl;
			set.push_back(aux);
		}
	}		
	archi.close();
	set.pop_back();
}

double functionFitness(vector<bool> chromosome){
	double gain = 0;
	double weight = 0;
	double fitness;
	int length = set.size();

	for(int i=0;i<length;i++){
		if(chromosome[i]){
			gain += set[i].g;
			weight += set[i].w;
		}
	}
	if(weight > maxW){
		fitness = gain - (weight-maxW)*penalization;
	}else{
		fitness = gain;	
	}
	return fitness;
}

